package com.mindainfo.groundwave.server.member.service;

import com.mindainfo.groundwave.common.api.member.param.EmpQueryListParam;
import com.mindainfo.groundwave.common.api.member.param.EmpSaveParam;
import com.mindainfo.groundwave.common.api.member.param.EmpToDelParam;
import com.mindainfo.groundwave.common.api.member.service.IDeptService;
import com.mindainfo.groundwave.common.api.member.service.IEmployeeService;
import com.mindainfo.groundwave.common.api.member.vo.EmpQueryListVo;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.ResultFactory;
import com.mindainfo.groundwave.common.bean.TableData;
import com.mindainfo.groundwave.server.member.bean.SysDept;
import com.mindainfo.groundwave.server.member.bean.SysEmployee;
import com.mindainfo.groundwave.server.member.bean.SysMember;
import com.mindainfo.groundwave.server.member.mapper.SysDeptMapper;
import com.mindainfo.groundwave.server.member.mapper.SysEmployeeMapper;
import com.mindainfo.groundwave.server.member.mapper.SysMemberMapper;
import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by MD-01 on 2017/7/3.
 */
@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {

    @Resource
    private IDeptService deptService;
    @Resource
    private SysEmployeeMapper employeeMapper;
    @Resource
    private SysDeptMapper deptMapper;
    @Resource
    private SysMemberMapper memberMapper;

    /**
     * 后台页面分页获取人员数据
     */
    public CommonResult<TableData<EmpQueryListVo>> queryList(EmpQueryListParam param) {

        //先获取当前用户的部门树
        CommonResult<List<Integer>> myDeptIdList = deptService.getMyDeptIdList();

        List<SysEmployee> sysEmployees = employeeMapper.queryList(param.getStart(), param.getNum(), param.getKey(), myDeptIdList.getData());

        List<EmpQueryListVo> vos = new ArrayList<>();
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
        for(SysEmployee emp : sysEmployees){
            EmpQueryListVo vo = new EmpQueryListVo();
            vo.setEmployeeId(emp.getId());
            vo.setName(emp.getName());
            vo.setPhoneNum(emp.getPhoneNum());
            vo.setPost(emp.getPost());
            vo.setCreateTime(format.format(emp.getCreateTime()));
            vo.setDeptId(emp.getDeptId());
            SysDept dept = deptMapper.selectById(emp.getDeptId());
            if(dept!=null){
                vo.setDeptName(dept.getName());
            }
            SysMember member = memberMapper.queryByEmpId(emp.getId());
            if(member==null){
                vo.setMemberId(0);
            }else{
                vo.setMemberId(member.getId());
                vo.setMemberName(member.getUsername());
            }
            vos.add(vo);
        }
        int count = employeeMapper.queryListCount(param.getKey(),myDeptIdList.getData());
        TableData<EmpQueryListVo> data = new TableData<>();
        data.setData(vos);
        data.setRecordsTotal(count);
        data.setRecordsFiltered(count);
        return ResultFactory.ok(data);
    }


    /**
     * 保存人员
     */
    public CommonResult<Void> save(EmpSaveParam param) {

        if(StringUtils.isEmpty(param.getName())){
            return ResultFactory.error("姓名不能为空");
        }
        if(StringUtils.isEmpty(param.getPhoneNum())){
            return ResultFactory.error("联系电话不能为空");
        }
        if(StringUtils.isEmpty(param.getPost())){
            return ResultFactory.error("岗位不能为空");
        }

        //新建
        if(param.getEmployeeId()==null){
            SysEmployee employee = new SysEmployee();
            employee.setCreateTime(new Date());
            employee.setDeptId(param.getDeptId());
            employee.setName(param.getName());
            employee.setPhoneNum(param.getPhoneNum());
            employee.setPost(param.getPost());
            employeeMapper.insert(employee);
            return ResultFactory.ok();
        }
        //修改
        SysEmployee employee = employeeMapper.selectById(param.getEmployeeId());
        if(employee==null){
            return ResultFactory.error("错误的人员编号");
        }
        employee.setPost(param.getPost());
        employee.setPhoneNum(param.getPhoneNum());
        employee.setName(param.getName());
        employee.setDeptId(param.getDeptId());
        employeeMapper.updateById(employee);
        return ResultFactory.ok();
    }

    /**
     * 删除人员
     */
    public CommonResult<Void> toDel(EmpToDelParam param) {

        SysEmployee employee = employeeMapper.selectById(param.getEmployeeId());
        if(employee==null){
            return ResultFactory.error("错误的人员编号");
        }
        SysMember member = memberMapper.queryByEmpId(param.getEmployeeId());
        if(member!=null){
            return ResultFactory.error("该人员添加了用户,不能删除.");
        }
        employeeMapper.deleteById(param.getEmployeeId());
        return ResultFactory.ok();
    }
}
